package com.rxh.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rxh.mapper.base.IdMapper;
import com.rxh.mapper.finance.FinanceBusinessMoneyMapper;
import com.rxh.mapper.finance.FinanceDrawingMapper;
import com.rxh.pojo.base.Page;
import com.rxh.pojo.base.PageResult;
import com.rxh.pojo.finance.FinanceBusinessMoney;
import com.rxh.pojo.finance.FinanceDrawing;
import com.rxh.service.ConstantService;
import com.rxh.service.FinanceService;
import com.rxh.utils.DateUtils;
import com.rxh.utils.SystemConstant;
import com.rxh.utils.UUID;
import org.apache.poi.ss.formula.functions.Finance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 陈俊雄
 * Date: 2018/7/13
 * Time: 17:06
 * Project: Management
 * Package: com.rxh.service.impl
 */
@Service
public class FinanceServiceImpl implements FinanceService {

    @Resource
    private IdMapper idMapper;
    @Resource
    private FinanceDrawingMapper financeDrawingMapper;
    @Resource
    private ConstantService constantService;
    @Resource
    private FinanceBusinessMoneyMapper financeBusinessMoneyMapper;


    @Override
    public Boolean addFinanceDrawing(Integer merchantId, Short type, String currency, BigDecimal amount, String transferCurrency, BigDecimal rate, String userName) {
        FinanceDrawing financeDrawing = new FinanceDrawing();
        financeDrawing.setId(UUID.createIntegerKey(idMapper.selectLastId("finance_drawing")));
        financeDrawing.setRefId(merchantId);
        financeDrawing.setType(type);
        financeDrawing.setCurrency(currency);
        financeDrawing.setAmount(amount);
        financeDrawing.setTransferAmount(amount.multiply(rate).setScale(2, BigDecimal.ROUND_HALF_UP));
        financeDrawing.setTransferCurrency(transferCurrency);
        financeDrawing.setRate(rate);
        financeDrawing.setApplicant(userName);
        financeDrawing.setApplicationTime(new Date());
        financeDrawing.setStatus(SystemConstant.FINANCE_DRAWING_STATUS_UNAUDITED);
        return financeDrawingMapper.insertSelective(financeDrawing) == 1;
    }

    @Override
    public PageResult getFinanceDrawing(Page page) {
        // 设置分页信息
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        if (page.getOrderBy() == null || page.getOrderBy().size() == 0) {
            PageHelper.orderBy("id DESC");
        } else {
            page.getOrderBy().forEach((key, value) -> PageHelper.orderBy(key + " " + value));
        }
        // 执行查询
        if (page.getSearchInfo() != null && page.getSearchInfo().getEndDate() != null) {
            page.getSearchInfo().setEndDate(DateUtils.setEndDate(page.getSearchInfo().getEndDate()));
        }

        List<FinanceDrawing> list = financeDrawingMapper.selectBySearchInfo(page.getSearchInfo());
        PageInfo<FinanceDrawing> pageInfo = new PageInfo<>(list);
        // 获取查询结果
        PageResult result = new PageResult();
        result.setRows(list);
        result.setTotal(pageInfo.getTotal());
        result.setAllPage(pageInfo.getPages());
        // 返回结果
        return result;
    }

    @Override
    public Map<String, Object> getFinanceDrawingInit() {
        Map<String, Object> init = new HashMap<>();
        init.put("financeDrawingStatus", constantService.getConstantByGroupName(SystemConstant.FINANCE_DRAWING_STATUS));
        return init;
    }

    public FinanceBusinessMoney getFinanceBusinessMoneyByRefId(int merId) {
        return financeBusinessMoneyMapper.getFinanceBusinessMoneyByRefId(merId);
    }

    // @Override
    // public int updateFinanceBusinessMoneyByEntity(FinanceBusinessMoney financeBusinessMoney) {
    //     return financeBusinessMoneyMapper.updateFinanceBusinessMoneyByEntity(financeBusinessMoney);
    // }

    public int updateFinanceBusinessMoneyByPrimaryKeySelective(FinanceBusinessMoney financeBusinessMoney) {
        return financeBusinessMoneyMapper.updateFinanceBusinessMoneyByPrimaryKeySelective(financeBusinessMoney);
    }
}